import ajaxApi from '@/modules/ajax-api/index.js'
export default {
  data () {
    return {
      // 箭头按钮
      selectArrow: '',
      // 下拉选择
      // 下拉选择
      selectedIndexs: [0, 0, 0],
      selectedValues: [
        { text: '全部宿舍楼', value: 0 },
        { text: '全部宿舍', value: 0 },
        { text: '今天', value: 0 }
      ],
      // 下拉选择临时
      selectedIndexsTemp: [0, 0, 0],
      selectedValuesTemp: [
        { text: '全部宿舍楼', value: 0 },
        { text: '全部宿舍', value: 0 },
        { text: '今天', value: 0 }
      ]
    }
  },
  methods: {
    // confirm () {
    //   this.selectedIndexs = this.selectedIndexsTemp.slice(0)
    //   let selectedValues = this.selectedValues = this.selectedValuesTemp.slice(0)
    //   console.log(
    //     {
    //       gradeId: selectedValues[0].value,
    //       classId: selectedValues[1].value
    //     }
    //   )
    // },
    async showSlideSelect () {
      if (this.selectArrow === 'up') {
        this.closeSlideSelect()
        return
      }
      this.selectArrow = 'up'
      let vSlideSelect = this.$refs.vSlideSelect
      vSlideSelect.show()
      let options = await this.getSelectOptions()
      vSlideSelect.setOptions(options)
    },
    closeSlideSelect () {
      // 还原
      this.selectedIndexsTemp = this.selectedIndexs.slice(0)
      this.selectedValuesTemp = this.selectedValues.slice(0)

      this.selectArrow = ''
      this.$refs.vSlideSelect.close()
    },
    async getSelectOptions () {
      let that = this
      let { selectedIndexsTemp, selectedValuesTemp } = that
      let areaList = await that.getAreaList()
      let dormList = that.getDormList(selectedIndexsTemp[0])
      let rangeList = that.getRangeList()
      return {
        // titles: ['年级', '班级'],
        lists: [areaList, dormList, rangeList],
        selectedIndexs: selectedIndexsTemp.slice(0), // 当前选择，通过重新赋值控制子组件选择
        async onChange (index, id, indexs, { value, text }) { // id 表示列
          selectedIndexsTemp[id] = index
          that.$set(selectedValuesTemp, id, {
            value,
            text
          })
          if (id === 0) {
            let classList = that.getDormList(index)
            this.setList(1, classList)
          }
        },
        onConfirm () {
          that.confirm()
        }
      }
    },
    async getAreaList () {
      let areaList = this.areaList = await ajaxApi.getDormList()
      let newList = areaList.map((d, i) => {
        return {
          text: d.building_name,
          value: d.building_id
        }
      })
      if (this.allSelectGradeClass) newList.unshift({ text: '[全部宿舍楼]', value: 0 })
      return newList
    },
    getDormList (areaIndex) {
      let { allSelectGradeClass } = this
      let newList = []
      let need = true
      if (allSelectGradeClass) {
        newList = [{ text: '[全部宿舍]', value: 0 }]
        if (areaIndex > 0) {
          areaIndex -= 1
        } else {
          need = false
        }
      }
      if (need) {
        this.areaList[areaIndex].dorm_list.forEach(d => {
          newList.push({
            text: d.dorm_name,
            value: d.dorm_id
          })
        })
      }
      return newList
    },
    getRangeList () {
      return [
        {
          value: 0,
          text: '今天'
        },
        {
          value: 1,
          text: '昨天'
        },
        {
          value: 2,
          text: '本周'
        },
        {
          value: 3,
          text: '上周'
        },
        {
          value: 4,
          text: '本月'
        },
        {
          value: 5,
          text: '上月'
        },
        {
          value: 6,
          text: '本学期'
        }
      ]
    }
    // async gradeClassFirstSelectedInit () {
    //   if (!this.allSelectGradeClass) {
    //     let gradeList = await this.getGradeList()
    //     let { selectedIndexs } = this
    //     let { 0: selectedIndexGrade, 1: selectedIndexClass } = selectedIndexs
    //     let classList = this.getClassList(selectedIndexGrade)
    //     let grade = gradeList[selectedIndexGrade]
    //     let cla = classList[selectedIndexClass]
    //     this.selectedValues = [
    //       { text: grade.text, value: grade.value },
    //       { text: cla.text, value: cla.value }
    //     ]
    //     this.selectedValuesTemp = this.selectedValues.slice(0)
    //   }
    // }
  }
}
